package cn.bugDance.domain.activity.service.partake;

import cn.bugDance.domain.activity.model.entity.ActivityEntity;
import cn.bugDance.domain.activity.model.entity.ActivityPartakeOrderEntity;
import cn.bugDance.domain.activity.model.entity.PartakeActivityFactorEntity;
import cn.bugDance.domain.activity.model.valobj.ActivityStateVO;
import cn.bugDance.domain.activity.repository.IActivityRepository;
import cn.bugDance.domain.activity.service.IRaffleActivityPartakeService;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.Date;


@Slf4j
public abstract class AbstractRaffleActivityPartakeService implements IRaffleActivityPartakeService {

    @Resource
    private IActivityRepository activityRepository;


    /**
     * 1. 参数校验
     * 2. 检查活动状态
     * 3. 校验用户是否有未消费的参与订单, 有则直接返回让用户消费
     * 4.
     * @param partakeActivityFactorEntity 参与要素
     * @return activityPartakeOrderEntity 活动参与订单
     */
    @Override
    public ActivityPartakeOrderEntity partakeActivity(PartakeActivityFactorEntity partakeActivityFactorEntity) {
        String userId= partakeActivityFactorEntity.getUserId();
        Long activityId = partakeActivityFactorEntity.getActivityId();

        if(StringUtils.isBlank(userId) || null == activityId){
            log.info("【活动服务层-RaffleActivityPartakeService】: 用户参与活动参数缺失");
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }


        ActivityEntity activityEntity = activityRepository.queryActivityByActivityId(activityId);
        if(activityEntity.getState() != ActivityStateVO.ACTIVE){
            log.info("【活动服务层-RaffleActivityPartakeService】: 活动未在线");
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }

        Date nowDate = new Date();
        if(nowDate.before(activityEntity.getBeginTime()) || nowDate.after(activityEntity.getEndTime())){
            log.info("【活动服务层-RaffleActivityPartakeService】: 未在活动时间");
            throw new AppException(ResponseCode.ACTIVITY_TIME_ERROR.getCode(), ResponseCode.ACTIVITY_TIME_ERROR.getInfo());
        }

        ActivityPartakeOrderEntity activityPartakeOrderEntity = activityRepository.queryActivityPartakeOrderEntity(userId, activityId);
        if(null != activityPartakeOrderEntity){
            activityPartakeOrderEntity.setStrategyId(activityEntity.getStrategyId());
            activityPartakeOrderEntity.setEndDateTime(activityEntity.getEndTime());
            return activityPartakeOrderEntity;
        }


        activityPartakeOrderEntity = createActivityPartakeOrder(partakeActivityFactorEntity, nowDate);
        activityPartakeOrderEntity.setStrategyId(activityEntity.getStrategyId());
        activityPartakeOrderEntity.setEndDateTime(activityEntity.getEndTime());

        return activityPartakeOrderEntity;
    }

    protected abstract ActivityPartakeOrderEntity createActivityPartakeOrder(PartakeActivityFactorEntity partakeActivityFactorEntity, Date nowDate);
}
